//
//  TravelNotesDetailViewController.m
//  旅游咨询
//
//  Created by 徐斌 on 15/4/13.
//  Copyright (c) 2015年 xubin. All rights reserved.
//

#import "TravelNotesDetailViewController.h"

@interface TravelNotesDetailViewController ()

@end

@implementation TravelNotesDetailViewController

- (void)viewWillAppear:(BOOL)animated
{
    [self.navigationController setNavigationBarHidden:NO];
    
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor lightGrayColor];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.translucent = NO;
    self.cellImage = [[UIImageView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
    self.cellImage.image = [UIImage imageNamed:@"bg_fabutieshi@3x.png"];
    self.detailTableView = [[UITableView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
    self.detailTableView.separatorStyle = NO;
    [self.view addSubview:self.detailTableView];
    self.detailTableView.delegate = self;
    self.detailTableView.dataSource = self;
    
    self.detailTableView.bounces = NO;
    
    self.modelArray = [NSMutableArray array];
//    NSLog(@"  --   %@", self.myId);
    NSLog(@" %@", self.myId);
  
    if (self.idModel.DetailID != nil) {
        self.myId = self.idModel.DetailID;
//        NSLog(@"wzl");
    }
    NSLog(@"self.model.deta %@", self.idModel.DetailID);
//    2386646738
    self.link = [NSString stringWithFormat:@"http://api.breadtrip.com/trips/%@/waypoints/", self.myId];
//    2388366775
//    2388366775
    
    UIActivityIndicatorView *trstActivityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    trstActivityIndicator.center = CGPointMake(self.view.frame.size.width / 2, (self.view.frame.size.height - 113) / 2);
    [self.view addSubview:trstActivityIndicator];
    [trstActivityIndicator startAnimating];
    
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
//    self.hidesBottomBarWhenPushed = NO;
    
    NSString *url_string = self.link;
    //[NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];  代表支持所有的接口类型
    
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];
       
        NSMutableDictionary *dic = responseObject;
        NSMutableArray *array = [dic objectForKey:@"days"];
        self.name = [dic objectForKey:@"name"];
        for (NSDictionary *dic1 in array) {
            NSMutableArray *array1 = [dic1 objectForKey:@"waypoints"];
            for (NSMutableDictionary *dic2 in array1) {
                TravelNotesDetailModel *model = [[TravelNotesDetailModel alloc]init];
                model.photo = [dic2 objectForKey:@"photo"];
                model.text = [dic2 objectForKey:@"text"];
                model.local_time = [dic2 objectForKey:@"local_time"];
                model.locationDic = [dic2 objectForKey:@"location"];
                model.day = [dic2 objectForKey:@"day"];
                model.photo_info = [dic2 objectForKey:@"photo_info"];
                [self.modelArray addObject:model];
            }
        }
        [trstActivityIndicator removeFromSuperview];
        [self.detailTableView reloadData];
    
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"detail失败==== %@",error);
        
    }];
    
    
//    UIActivityIndicatorView *testActivityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
//    testActivityIndicator.center = CGPointMake(self.view.frame.size.width / 2.0, (self.view.frame.size.height - 113) / 2);//只能设置中心，不能设置大小
//    [self.view addSubview:testActivityIndicator];
//    [testActivityIndicator startAnimating]; // 开始旋转
//    testActivityIndicator.backgroundColor = [UIColor lightGrayColor];
    
  
    
    
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *cellFirl = @"cell";
    TravelDetaileTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellFirl];
    if (cell == nil) {
        cell = [[TravelDetaileTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellFirl];
    }
 
    cell.detailModel = [self.modelArray objectAtIndex:indexPath.row];
    cell.backgroundView = self.cellImage;
    cell.name = self.name;
    return cell;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    
    return self.modelArray.count;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TravelDetaileTableViewCell *cella = [[TravelDetaileTableViewCell alloc]init];

    
    if ([[[self.modelArray objectAtIndex:indexPath.row] photo_info] class] != [NSNull class]) {
        ;

        return [[cella class] getHeighContent:[[self.modelArray objectAtIndex:indexPath.row] text]] + [[[[self.modelArray objectAtIndex:indexPath.row] photo_info] objectForKey:@"h"] floatValue] / ([[[[self.modelArray objectAtIndex:indexPath.row] photo_info] objectForKey:@"w"] floatValue] / WidthZ *0.946667) + 30;
    }
    else
    {

        return [[cella class] getHeighContent:[[self.modelArray objectAtIndex:indexPath.row] text]] + 20;
    }
 
    
}
    
//    NSLog(@"--- %f", [[cella class] getHeighContent:[[self.modelArray objectAtIndex:indexPath.row] text]] + 200);
    
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
     if ([[[self.modelArray objectAtIndex:indexPath.row] photo_info] class] != [NSNull class])
     {
//        TravelImageViewController *imageVC = [[TravelImageViewController alloc]init];
////         imageVC.navigationController.navigationBar.Hidden = YES;
////         [imageVC.navigationController setNavigationBarHidden:YES animated:YES];
//         
//        
//        [self.navigationController pushViewController:imageVC animated:YES];
//
//        imageVC.modelArray = self.modelArray;
//        imageVC.index = indexPath.row;
//
         
         ImageViewController *photoVC = [[ImageViewController alloc]init];
         photoVC.myCollectionView.contentOffset = CGPointMake(indexPath.row * WidthZ, 0);
         [self.navigationController pushViewController:photoVC animated:YES];
//         [photoVC.navigationController setNavigationBarHidden:YES];
         photoVC.modelArray = self.modelArray;
         photoVC.index = indexPath.row;
//
         
         
     }
    else
    {
        
    }
    
}
- (void)pushViewController: (UIViewController*)controller
    animatedWithTransition: (UIViewAnimationTransition)transition
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.2];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(pushAnimationDidStop)];
    [UIView setAnimationTransition:transition forView:self.view cache:YES];
    [UIView commitAnimations];
}
- (void)pushAnimationDidStop {
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
